package com.example.myapplication.rxjava;

import android.os.Bundle;
import android.os.PersistableBundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.orhanobut.logger.Logger;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BooleanSupplier;
import io.reactivex.functions.Function;

/**
 * 类描述:
 * 创建人:   tangchao$
 * 创建时间: 2021/9/8$ 8:35 AM$
 * 联系方式: 419704299@qq.com
 * 修改人:   tangchao$
 * 修改时间: 2021/9/8$ 8:35 AM$
 * 修改备注:  [说明本次修改内容]
 */
public class RxjavaActivity08 extends AppCompatActivity {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);

        test08();
    }

    private void test08() {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Object> emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onError(new Exception("404"));

            }
        }).retryWhen(new Function<Observable<Throwable>, ObservableSource<?>>() {
            @Override
            public ObservableSource<?> apply(@NonNull Observable<Throwable> throwableObservable) throws Exception {
                return throwableObservable.flatMap(new Function<Throwable, ObservableSource<?>>() {
                    @Override
                    public ObservableSource<?> apply(@NonNull Throwable throwable) throws Exception {
                        if (throwable.getMessage().equals("404")) {
                            return Observable.just("可以忽略的异常");
                        } else {
                            return Observable.error(new Throwable("异常"));
                        }
                    }

                });
            }
        })

//                .retryUntil(new BooleanSupplier() {//供应商
//                    @Override
//                    public boolean getAsBoolean() throws Exception {
//                        return false;
//                    }
//                })


                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Object o) {
                        Logger.d("RxjavaActivity08===onNext: o=" + o);

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Logger.e("RxjavaActivity08===: " + e.getMessage());

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
